package com.hieu.tripmanager;

import java.util.ArrayList;

import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.hieu.bean.Map;
import com.hieu.sql.DAL;
import com.hieu.util.Util;

public class ViewMapActivity extends FragmentActivity implements
		LocationListener {
	private LocationManager mLocationManager;
	private GoogleMap mMap;
	private DAL mDAL;
	private Util util = new Util();
	String idTrip = null;
	private ArrayList<Map> mList = new ArrayList<Map>();
	private ArrayList<MarkerOptions> arrayMarker = new ArrayList<MarkerOptions>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_map_activity_layout);
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		mDAL = new DAL(this);

		Intent intent = getIntent();
		if (intent != null) {
			idTrip = intent.getStringExtra("idTrip");
		}
		if (idTrip == null) {
			if (mDAL != null) {
				idTrip = mDAL.getIdNotFinishTrip();
			}
		}
		mList = mDAL.getMap(idTrip);
		if (mList != null) {
			setUpMapIfNeeded(mList);
		} else {
			setUpMapDefault();
			Toast.makeText(this,
					"Hiện tại không có dữ liệu cho nhật ký bản đồ",
					Toast.LENGTH_SHORT).show();
		}
	}

	private void setUpMapIfNeeded(ArrayList<Map> mList) {
		// Do a null check to confirm that we have not already instantiated the
		// map.
		if (mMap == null) {
			// Try to obtain the map from the SupportMapFragment.
			mMap = ((SupportMapFragment) getSupportFragmentManager()
					.findFragmentById(R.id.map)).getMap();
			// Check if we were successful in obtaining the map.
			if (mMap != null) {
				ArrayList<MarkerOptions> arrayMarker = getListMarker(mList);
				if (arrayMarker.size() > 0) {
					mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(arrayMarker.get(0).getPosition(), 14.0f));
					mMap.setMyLocationEnabled(true);
					for (int i = 0; i < arrayMarker.size(); i++) {
						mMap.addMarker(arrayMarker.get(i));
					}
				} else {
					setUpMapDefault();
					Toast.makeText(this,
							"Chuyến đi không có dữ liệu nhật ký tọa độ",
							Toast.LENGTH_SHORT).show();
				}

			}
		}
	}
	
	private void setUpMapDefault() {
		// Do a null check to confirm that we have not already instantiated the
		// map.
		if (mMap == null) {
			// Try to obtain the map from the SupportMapFragment.
			mMap = ((SupportMapFragment) getSupportFragmentManager()
					.findFragmentById(R.id.map)).getMap();
			// Check if we were successful in obtaining the map.
			if (mMap != null) {
				mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
				mLocationManager.requestLocationUpdates(
						LocationManager.GPS_PROVIDER, 100, 1, this);
				mMap.setMyLocationEnabled(true);

			}
		}
	}

	public ArrayList<MarkerOptions> getListMarker(ArrayList<Map> mList) {
		for (int i = 0; i < mList.size(); i++) {
			if (mList.get(i).getPosition() != null) {
				BitmapDescriptor icon = BitmapDescriptorFactory
						.fromResource(R.drawable.icon_app);

				LatLng latLng = util.getLatLng(mList.get(i).getPosition());
				MarkerOptions markerOptions = new MarkerOptions()
						.position(latLng).title("" + i)
						.snippet(mList.get(i).getDes()).icon(icon);
				arrayMarker.add(markerOptions);
			}
		}
		Log.v("listMarker", "" + arrayMarker.size());
		return arrayMarker;

	}

	@Override
	public void onLocationChanged(Location location) {
		LatLng latLng = new LatLng(location.getLatitude(),
				location.getLongitude());
		CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng,
				15);
		mMap.animateCamera(cameraUpdate);
		mLocationManager.removeUpdates(this);
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
	}

	@Override
	public void onProviderEnabled(String provider) {
	}

	@Override
	public void onProviderDisabled(String provider) {
	}
}
